'use strict'
let underscore = require('underscore')
let templateConfig = require('../../config/templateConfig')
let ItemComponent = require('../component/itemComponent')
let fieldBossComponent = require('../component/fieldBossComponent')
let createMonsterHandle = require('../core/createMonsterHandle')
let attack = require('./attack')
let utils = require('../utils/utils')
let moneyAndExpConfig = templateConfig.other[290002]
let monsterDropSkills = []
exports.attackNormalMonster = function(player) {
  let lvl = randLvl(player.basicInfo.lvl)
  let monsterNames = templateConfig.other[290034][lvl]
  let monsterName = utils.getOneFromArray(monsterNames)
  let monster = createMonsterHandle.createNormalMonster(lvl, monsterName)
  let fightMsg = attack.attacks(player, monster)

  if (fightMsg.result === 1) {
    let secretMapId = player.basicInfo.secretMapId
    let lucky = player.fightAttri.lucky
    let dropEquipment = createMonsterHandle.createEquipment(lvl, 1, secretMapId, lucky) //掉落的装备
    if (dropEquipment) {
      dropEquipment.name = monsterName

      if (monster.fightAttri.attackSkill.length > 0 && dropEquipment.templateId === 600000) { //魂器
        dropEquipment.skills = monster.fightAttri.attackSkill
      }
    }
    //掉落的金钱
    let money = moneyAndExpConfig.money[0] * lvl + moneyAndExpConfig.money[1]
      //获得的经验
    let getExp = moneyAndExpConfig.exp[0] * lvl + moneyAndExpConfig.exp[1]
    let info = {
      dropEquipment: dropEquipment,
      money: money,
      getExp: getExp
    }
    calcGetThings(info, player) //结算金钱,掉落道具装备,经验
    fightMsg['equipment'] = dropEquipment
    fightMsg['exp'] = getExp
    fightMsg['money'] = info.money
  }
  return fightMsg
}

exports.attackMonster = function(player, monsterName) {
  let lvl = randLvl(player.basicInfo.lvl)
  let monster = createMonsterHandle.createNormalMonster(lvl, monsterName)
  let fightMsg = attack.attacks(player, monster)
  if (fightMsg.result === 1) {
    //掉落的金钱
    let money = moneyAndExpConfig.money[0] * lvl + moneyAndExpConfig.money[1]
      //获得的经验
    let getExp = moneyAndExpConfig.exp[0] * lvl + moneyAndExpConfig.exp[1]
    let info = {
      money: money,
      getExp: getExp
    }
    calcGetThings(info, player) //结算金钱,掉落道具装备,经验
    fightMsg['exp'] = getExp
    fightMsg['money'] = info.money
  }
  return fightMsg
}

//副本怪物
exports.attackCaveMonster = function(player, monsterId) {
  let monster = createMonsterHandle.createCaveMonster(monsterId)
  let fightMsg = attack.attacks(player, monster)
  if (fightMsg.result === 1) {
    let lvl = monster.fightAttri.lvl
    let monsterConfig = templateConfig.monster[monsterId]
    let secretMapId = player.basicInfo.secretMapId
    let lucky = player.fightAttri.lucky
    let dropEquipment = createMonsterHandle.createEquipment(lvl, 2, secretMapId, lucky) //掉落的装备
    if (dropEquipment) {
      dropEquipment.name = monster.name
    }
    let dropProps = monsterConfig.dropProp
    let dropProp = utils.getOneFromArray(dropProps)
    let money = moneyAndExpConfig.money[0] * lvl + moneyAndExpConfig.money[1] //掉落的金钱
    let getExp = moneyAndExpConfig.exp[0] * lvl + moneyAndExpConfig.exp[1] //获得的经验
    let info = {
      dropEquipment: dropEquipment,
      dropProp: dropProp,
      money: money,
      getExp: getExp
    }
    if (dropEquipment && dropEquipment.templateId === 600000) { //魂器
      let dropSkills = monsterConfig.dropSkills
      let len = dropSkills.length > 3 ? 3 : dropSkills.length
      let num = utils.getRandomNumber(1, len)
      let skills = utils.getRandomArray(num, dropSkills)
      dropEquipment.skills = skills
    }
    calcGetThings(info, player) //结算金钱,掉落道具装备,经验
    fightMsg['equipment'] = dropEquipment
    fightMsg['prop'] = dropProp
    fightMsg['exp'] = getExp
    fightMsg['money'] = info.money
  }
  return fightMsg
}

//魔窟怪物
exports.attackDevilCaveMonster = function(player, monsterId) {
  let monster = createMonsterHandle.createDevilCaveMonster(monsterId)
  let fightMsg = attack.attacks(player, monster)
  let monsterConfig = templateConfig.monster[monsterId]
  if (fightMsg.result === 1) {
    let lvl = monster.fightAttri.lvl
    let secretMapId = player.basicInfo.secretMapId
    let lucky = player.fightAttri.lucky
    let dropEquipment = createMonsterHandle.createEquipment(lvl, 3, secretMapId, lucky) //掉落的装备
    if (dropEquipment) {
      dropEquipment.name = monster.name
    }
    let dropProps = monsterConfig.dropProp
    let dropProp = null
    if (Math.random() * 100 < 100) {
      dropProp = utils.getOneFromArray(dropProps)
    }
    let money = moneyAndExpConfig.money[0] * lvl + moneyAndExpConfig.money[1] //掉落的金钱
    let getExp = moneyAndExpConfig.exp[0] * lvl + moneyAndExpConfig.exp[1] //获得的经验
    let info = {
      dropEquipment: dropEquipment,
      dropProp: dropProp,
      money: money,
      getExp: getExp
    }
    if (dropEquipment && dropEquipment.templateId === 600000) { //魂器
      let dropSkills = monsterConfig.dropSkills
      let len = dropSkills.length > 3 ? 3 : dropSkills.length
      let num = utils.getRandomNumber(1, len)
      let skills = utils.getRandomArray(num, dropSkills)
      dropEquipment.skills = skills
    }
    calcGetThings(info, player) //结算金钱,掉落道具装备,经验
    fightMsg['equipment'] = info.dropEquipment
    fightMsg['prop'] = info.dropProp
    fightMsg['exp'] = info.getExp
    fightMsg['money'] = info.money
  }
  return fightMsg
}

//攻击强盗
exports.attackRobber = function(player) {
  let robber = createMonsterHandle.createRobber(player.basicInfo.lvl)
  let fightMsg = attack.attacks(player, robber)
  return fightMsg
}

//攻击野外boss
exports.attackFieldBoss = function(player, fieldBoss) {
  let fightMsg = attack.attackFieldBoss(player, fieldBoss)
  return fightMsg
}

//玩家对战
exports.playerBattle = function(player1, player2) {
  let fightMsg = attack.attackPlayer(player1, player2)
  return fightMsg
}

function randLvl(lvl) {
  let rand = Math.random() * 100
  if (rand < 20) {
    lvl += 1
    return lvl > 5 ? 5 : lvl
  } else if (rand < 30) {
    lvl -= 1
    return lvl < 1 ? 1 : lvl
  }
  return lvl
}

//结算金钱,掉落道具装备,经验
function calcGetThings(info, player) {
  let dropEquipment = info.dropEquipment
  if (dropEquipment) {
    player.adventureBag.push(dropEquipment) //添加物品到背包
  }
  let dropPropId = info.dropProp
  if (dropPropId) {
    let dropPropComponent = templateConfig.total[dropPropId].getItemComponent()
    player.adventureBag.push(dropPropComponent) //添加物品到背包
  }
  if (info.money) {
    info.money = parseInt(info.money * (1 + player.fightAttri.money / 100))
    player.basicInfo.money += info.money //添加金钱
  }
  if (player.basicInfo.lvl <= 4 && info.getExp) {
    player.lvlup(info.getExp) //添加经验
  } else {
    info.getExp = 0
  }
}
